package com.atguigu.eduservice.service.impl;

import com.atguigu.commonutils.ResultCode;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduCourseDescription;
import com.atguigu.eduservice.entity.vo.CourseInfoVO;
import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.service.EduCourseDescriptionService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author fly
 * @since 2021-10-08
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    /**
     * 添加课程
     */
    @Override
    public void saveCourseInfo(CourseInfoVO courseInfoVo) {
        // 1.添加课程基本信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        boolean save = this.save(eduCourse);
        if (!save) {
            throw new GuliException(ResultCode.ERROR, "添加课程失败");
        }

        // 1.添加课程描述信息
        EduCourseDescription eduCourseDescription = EduCourseDescription.builder()
                // 手动设置eduCourseDescription的id为eduCourse的id，以形成1对1关系
                .id(eduCourse.getId())
                .description(courseInfoVo.getDescription())
                .build();
        boolean save2 = eduCourseDescriptionService.save(eduCourseDescription);
        if (!save2) {
            throw new GuliException(ResultCode.ERROR, "添加课程失败");
        }
    }
}
